package cn.school.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.school.common.rabbitMQ.RabbitSenderService;
import cn.school.common.utils.DateTimeUtils;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.dao.StudentLeaveReportSchoolDao;
import cn.school.student.entity.StudentInfo;
import cn.school.student.entity.TStudentLeaveReportSchool;
import cn.school.student.service.StudentLeaveReportService;
import cn.school.student.vo.StudentLeaveInfoVo;
import cn.school.workflow.dao.StudentLeaveApplyDao;
import cn.school.workflow.dao.StudentLeaveSelfApplyDao;
import cn.school.workflow.vo.StudentLeaveApplyVO;
import cn.school.workflow.vo.StudentLeaveSelfApplyVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 学生销假业务
 * @ClassName: StudentLeaveReportServiceImpl
 * @Author: tzw
 * @Date: 2024/3/28 12:48
 */
@Service
public class StudentLeaveReportServiceImpl implements StudentLeaveReportService {

    @Resource
    private StudentLeaveReportSchoolDao studentLeaveReportSchoolDao;
    @Resource
    private StudentInfoDao studentInfoDao;
    @Resource
    private RabbitSenderService rabbitSenderService;
    @Resource
    private StudentLeaveSelfApplyDao studentLeaveSelfApplyDao;
    @Resource
    private StudentLeaveApplyDao studentLeaveApplyDao;


    @Override
    public IPage<StudentLeaveApplyVO> pagelist(StudentLeaveApplyVO studentLeaveApplyVO) {
        LambdaQueryWrapper<StudentLeaveApplyVO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(studentLeaveApplyVO.getApplyNo()),StudentLeaveApplyVO::getApplyNo,studentLeaveApplyVO.getApplyNo());
        queryWrapper.eq(!ObjectUtils.isEmpty(studentLeaveApplyVO.getLeaveType()),StudentLeaveApplyVO::getLeaveType,studentLeaveApplyVO.getLeaveType());
        Page<StudentLeaveApplyVO> page = new Page<>(studentLeaveApplyVO.getPageNum(), studentLeaveApplyVO.getPageSize());
        Page<StudentLeaveApplyVO> page1 = studentLeaveApplyDao.selectPage(page, queryWrapper);
        return page1;
    }

    @Override
    public String createLeaveReport(String applyNo) {
        TStudentLeaveReportSchool studentLeaveInfo = new TStudentLeaveReportSchool();

        LambdaQueryWrapper<StudentLeaveSelfApplyVO> queryLeaveSelf = new LambdaQueryWrapper<>();
        queryLeaveSelf.eq(StudentLeaveSelfApplyVO::getApplyNo, applyNo);
        StudentLeaveSelfApplyVO leaveSelfApplyVO = studentLeaveSelfApplyDao.selectOne(queryLeaveSelf);

        LambdaQueryWrapper<StudentLeaveApplyVO> queryLeave = new LambdaQueryWrapper<>();
        queryLeave.eq(StudentLeaveApplyVO::getApplyNo, applyNo);
        StudentLeaveApplyVO leaveApplyVO = studentLeaveApplyDao.selectOne(queryLeave);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = format.format(new Date());
        if (leaveSelfApplyVO != null && !ObjectUtil.isEmpty(leaveSelfApplyVO)){
            if (StringUtils.isNotBlank(leaveSelfApplyVO.getIsReport())){
                return "该学生已销假";
            }
            //studentId值修改为student_info表主键id
            LambdaQueryWrapper<StudentInfo> queryStudent = new LambdaQueryWrapper<>();
            queryStudent.eq(StudentInfo::getStudentId, leaveSelfApplyVO.getStudentId());
            StudentInfo studentInfo = studentInfoDao.selectOne(queryStudent);
            studentLeaveInfo.setStudentId(String.valueOf(studentInfo.getId()));

            studentLeaveInfo.setLeaveStartTime(leaveSelfApplyVO.getLeaveStartTime());
            studentLeaveInfo.setLeaveEndTime(leaveSelfApplyVO.getLeaveEndTime());
            studentLeaveInfo.setClassId(leaveSelfApplyVO.getClassId());
            studentLeaveInfo.setTenantId(leaveSelfApplyVO.getTenantId());
            studentLeaveInfo.setReportAfterSchoolTime(nowDate);
            sendLeaveMessageToIot(studentLeaveInfo);
            studentLeaveReportSchoolDao.insert(studentLeaveInfo);
            //更新请假自提表
            leaveSelfApplyVO.setIsReport("1");
            leaveSelfApplyVO.setLeaveEndTime(nowDate);
            studentLeaveSelfApplyDao.update(leaveSelfApplyVO, queryLeaveSelf);
            return "学生销假成功";
        }

        if (leaveApplyVO != null && !ObjectUtil.isEmpty(leaveApplyVO)){
            if (StringUtils.isNotBlank(leaveApplyVO.getIsReport())){
                return "该学生已销假";
            }
            studentLeaveInfo.setStudentId(leaveApplyVO.getStudentId());
            studentLeaveInfo.setLeaveStartTime(leaveApplyVO.getLeaveStartTime());
            studentLeaveInfo.setLeaveEndTime(leaveApplyVO.getLeaveEndTime());
            studentLeaveInfo.setClassId(leaveApplyVO.getClassId());
            studentLeaveInfo.setTenantId(leaveApplyVO.getTenantId());
            studentLeaveInfo.setReportAfterSchoolTime(nowDate);
            sendLeaveMessageToIot(studentLeaveInfo);
            studentLeaveReportSchoolDao.insert(studentLeaveInfo);
            //更新请假表
            leaveApplyVO.setIsReport("1");
            leaveApplyVO.setLeaveEndTime(nowDate);
            studentLeaveApplyDao.update(leaveApplyVO, queryLeave);
            return "学生销假成功";
        }
       return "学生销假失败";
    }



    private void sendLeaveMessageToIot(TStudentLeaveReportSchool leaveVO) {
        LambdaQueryWrapper<StudentInfo> studentInfoWrapper = new LambdaQueryWrapper<>();
        studentInfoWrapper.eq(StudentInfo::getStudentId, leaveVO.getStudentId());
        StudentInfo studentInfo = studentInfoDao.selectOne(studentInfoWrapper);
        if (ObjectUtil.isNull(studentInfo)){
            studentInfoWrapper.clear();
            studentInfoWrapper.eq(StudentInfo::getId, leaveVO.getStudentId());
            studentInfo = studentInfoDao.selectOne(studentInfoWrapper);
        }
        leaveVO.setStudentInfo(studentInfo);
        String tenantId = studentInfo.getTenantId();
        String exchange = "zhxypt-"+tenantId+"-exchanges";
        String routingKey = "zhxypt-"+tenantId+"-routing";
        //发送消息给MQ TODO
        rabbitSenderService.sendMqMessage(JSON.toJSONString(leaveVO), null,exchange,routingKey);
    }

}
